www.gusucode.com > VC 串口封装类(支持同步与异步)-源码程序 > VC 串口封装类(支持同步与异步)-源码程序/code/CommTextDlg.cpp
//Download by http://www.NewXing.com // CommTextDlg.cpp : implementation file // #include "stdafx.h" #include "CommText.h" #include "CommTextDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About extern CCommTextApp theApp; DWORD WINAPI ThreadCommProc( LPVOID lpParameter); class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCommTextDlg dialog CCommTextDlg::CCommTextDlg(CWnd* pParent /*=NULL*/) : CDialog(CCommTextDlg::IDD, pParent) { //{{AFX_DATA_INIT(CCommTextDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CCommTextDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCommTextDlg) DDX_Control(pDX, IDC_EDIT2, m_str2); DDX_Control(pDX, IDC_EDIT1, m_str1); DDX_Control(pDX, IDC_COMBO2, m_com2); DDX_Control(pDX, IDC_COMBO1, m_com1); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCommTextDlg, CDialog) //{{AFX_MSG_MAP(CCommTextDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON2, OnButton2) ON_WM_CLOSE() ON_BN_CLICKED(IDC_BUTTON, OnButton) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCommTextDlg message handlers BOOL CCommTextDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_com1.AddString("COM1"); m_com1.AddString("COM2"); m_com2.AddString("COM1"); m_com2.AddString("COM2"); bThreadStart = FALSE; return TRUE; // return TRUE unless you set the focus to a control } void CCommTextDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CCommTextDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CCommTextDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } DWORD WINAPI ThreadCommProc( LPVOID lpParameter) { CCommTextDlg *pParentWnd; pParentWnd=(CCommTextDlg *) lpParameter; CString m_str; char CommPort[20], comm_name[20], showstr[1024]; BYTE buf[1024]; DWORD dLen; memset(buf, 0, 1024); memset(showstr, 0, 1024); memset(CommPort, 0, 20); memset(comm_name, 0, 20); strcpy(CommPort,"\\\\.\\"); pParentWnd->m_com2.GetWindowText(comm_name, 20); strcat(CommPort,comm_name); theApp.m_com2.open(CommPort, 9600, 1); // 异步方式打开 //设置字符事件 theApp.m_com2.SetEventChar(0x02); //清串口 theApp.m_com2.PurgeIn(); //设置发送请求线为低 theApp.m_com2.SetRTS(0); while(WaitForSingleObject(pParentWnd->ExitEvent,0)==WAIT_TIMEOUT) { if (!theApp.m_com2.WaitEventChar()) continue; theApp.m_com2.Read(buf, 1024); pParentWnd->m_str2.SetWindowText((char *)buf); } SetEvent(pParentWnd->ExitedEvent); return 0; } void CCommTextDlg::OnButton2() { // TODO: Add your control notification handler code here ExitEvent=CreateEvent( NULL, // no security TRUE, // explicit reset req FALSE, // initial event reset NULL ) ; // no name ExitedEvent=CreateEvent( NULL, // no security TRUE, // explicit reset req FALSE, // initial event reset NULL ) ; // no name if ((hThread=CreateThread(NULL,0,ThreadCommProc,this,0,&ThreadId))==NULL) { CloseHandle(ExitEvent); CloseHandle(ExitedEvent); MessageBox("创建通讯线程失败","错误信息",MB_OK); return; } bThreadStart = TRUE; hProc=GetCurrentProcess(); SetPriorityClass(hProc,HIGH_PRIORITY_CLASS); SetThreadPriority(hThread,THREAD_PRIORITY_TIME_CRITICAL); } void CCommTextDlg::OnClose() { // TODO: Add your message handler code here and/or call default if (bThreadStart) { theApp.m_com1.Close(); SetEvent(ExitEvent); WaitForSingleObject(ExitedEvent,INFINITE); CloseHandle(ExitedEvent); CloseHandle(ExitEvent); } CDialog::OnClose(); } void CCommTextDlg::OnButton() { // TODO: Add your control notification handler code here char CommPort[20], comm_name[20], sendstr[1024]; CString str; memset(sendstr, 0, 1024); memset(CommPort, 0, 20); memset(comm_name, 0, 20); strcpy(CommPort,"\\\\.\\"); m_com1.GetWindowText(comm_name, 20); strcat(CommPort,comm_name); m_str1.GetWindowText(sendstr, 1024); str = sendstr; theApp.m_com1.open(CommPort, 9600, 0); theApp.m_com1.SendStrCommand(str); theApp.m_com1.Close(); }